package dev.ece.util.tools;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 自增长工具
 * @author 潜行的虚影
 *
 */
public class AutoIncrement {

	public static interface AutoIncrementListener<T extends Number> {
		
		public void onAnimate(T[] result, boolean finished);
		
	}
	
	public static void animate(int time, int speed, final int[] start, final int[] end, AutoIncrementListener<Integer> listener) {
		if(end.length > start.length) {
			throw new IllegalArgumentException("end.length不能大于start.length");
		}
		if(listener == null) {
			throw new IllegalArgumentException("listener不能是null");
		}
		final int times = time / speed;
		int[] increment = new int[end.length];
		for(int i=0; i<end.length; i++) {
			increment[i] = (end[i] - start[i]) / times;
		}
		final ExecutorService service = Executors.newSingleThreadExecutor();
		service.execute(new Runnable() {

			@Override
			public void run() {
				Integer[] result = new Integer[start.length];
				for(int i=0; i<start.length; i++) {
					result[i] = start[i];
				}
				for(int i=0; i<times; i++) {
					for(int j=0; j<end.length; j++) {
						result[j] += increment[j];
					}
					try {
						Thread.sleep(speed);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					try {
						listener.onAnimate(result, false);
					} catch(Exception e) {
						e.printStackTrace();
					}
				}
				for(int i=0; i<end.length; i++) {
					result[i] = end[i];
				}
				try {
					service.shutdown();
					listener.onAnimate(result, true);
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
			
		});
	}
	
	public static void animate(int time, int speed, final float[] start, final float[] end, AutoIncrementListener<Float> listener) {
		if(end.length > start.length) {
			throw new IllegalArgumentException("end.length不能大于start.length");
		}
		if(listener == null) {
			throw new IllegalArgumentException("listener不能是null");
		}
		final int times = time / speed;
		float[] increment = new float[end.length];
		for(int i=0; i<end.length; i++) {
			increment[i] = (end[i] - start[i]) / times;
		}
		final ExecutorService service = Executors.newSingleThreadExecutor();
		service.execute(new Runnable() {

			@Override
			public void run() {
				Float[] result = new Float[start.length];
				for(int i=0; i<start.length; i++) {
					result[i] = start[i];
				}
				for(int i=0; i<times; i++) {
					for(int j=0; j<end.length; j++) {
						result[j] += increment[j];
					}
					try {
						Thread.sleep(speed);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					try {
						listener.onAnimate(result, false);
					} catch(Exception e) {
						e.printStackTrace();
					}
				}
				for(int i=0; i<end.length; i++) {
					result[i] = end[i];
				}
				try {
					service.shutdown();
					listener.onAnimate(result, true);
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
			
		});
	}
	
}
